package com.lcb.jmm.unsafe;

import com.lcb.jmm.volatiledome.UnsafeInstance;
import sun.misc.Unsafe;

/****
 * @Author:刘长柏
 * @Date: 2021-01-05 16:09
 * @Description: 操作内存
 *****/
public class DirectMemoryAccessTest {

    public static void main(String[] args) {

        Unsafe unsafe = UnsafeInstance.reflectGetUnsafe();

        long oneHundred = 1;
        byte size = 1;

        /*
         * 调用allocateMemory分配内存
         */
        long memoryAddress = unsafe.allocateMemory(size);

        /*
         * 将1写入到内存中
         */
        unsafe.putAddress(memoryAddress, oneHundred);

        /*
         * 内存中读取数据
         */
        long readValue = unsafe.getAddress(memoryAddress);

        System.out.println("value : " + readValue);
    }
}
